//
//  MTClubTool.swift
//  MT
//
//  Created by ooops on 2019/12/25.
//  Copyright © 2019 张强. All rights reserved.
//

import UIKit
/// 网络请求

import RxSwift
import RxDataSources
import Moya
import Result
import MTCategory

public enum UpdateMemberType: Int {
    /// 更新俱乐部成员
    case club
    /// 更新参加俱乐部活动成员
    case clubActivity
}

public class MTClubTool: NSObject {
    public override init() {
        super.init()
    }
}
// MARK: - MTClubApi 再上一层的封装
extension MTClubTool {
    // MARK: - 操作俱乐部成员的封装
    
    class func operateClubUsers(provider: MTProvider<MTClubApi>,
                                operateType: UpdateMemberType,
                                requestParam: [String : Any],
                                complete: @escaping (BaseComponentModel?) -> Swift.Void,
                                failure: @escaping (AnyError) -> Swift.Void) {
        
        if operateType == .club {
            self.operateClubUser(provider: provider, requestParam: requestParam, complete: complete, failure: failure)
        } else {
            self.operateClubActivityUser(provider: provider, requestParam: requestParam, complete: complete, failure: failure)
        }
    }
    
}


extension MTClubTool {
    
    /// 编辑俱乐部活动
    /// - Parameters:
    ///   - provider: provider description
    ///   - requestParam: 请求参数
    ///   - complete: complete description
    ///   - failure: failure description
    class func editClubActivity(provider: MTProvider<MTClubApi>,
                                requestParam: [String : Any],
                                complete: @escaping (BaseComponentModel?) -> Swift.Void,
                                failure: @escaping (AnyError) -> Swift.Void) {
        provider.rx.requestResult(.editClubActivity(requestParam)).subscribe(onNext: { (result:Result<BaseComponentModel, AnyError> , isCache:Bool) in
            switch result {
            case .success(let model):
                complete(model)
            case .failure(let error):
                failure(error)
                break
            }
            
        }).disposed(by: globalDisposedBag)
        
    }
    
    /// 创建俱乐部活动
    /// - Parameters:
    ///   - provider: provider description
    ///   - requestParam: 请求参数
    ///   - complete: complete description
    ///   - failure: failure description
    class func createClubActivities(provider: MTProvider<MTClubApi>,
                                    requestParam: [String : Any],
                                    complete: @escaping (BaseComponentModel?) -> Swift.Void,
                                    failure: @escaping (AnyError) -> Swift.Void) {
        provider.rx.requestResult(.createClubActivities(requestParam)).subscribe(onNext: { (result:Result<BaseComponentModel, AnyError> , isCache:Bool) in
            switch result {
            case .success(let model):
                complete(model)
            case .failure(let error):
                failure(error)
                break
            }
            
        }).disposed(by: globalDisposedBag)
        
    }
    
    /// 创建俱乐部
    /// - Parameters:
    ///   - requestParam: 请求参数
    ///   - callBack: 回调
    class func createClub(provider: MTProvider<MTClubApi>,
                          requestParam: [String : Any],
                          complete: @escaping (MTClubRichListModel?) -> Swift.Void,
                          failure: @escaping (AnyError) -> Swift.Void) {
        provider.rx.requestResult(.createClub(requestParam)).subscribe(onNext: { (result:Result<MTClubRichListModel, AnyError> , isCache:Bool) in
            switch result {
            case .success(let m):
                complete(m)
            case .failure(let error):
                failure(error)
                break
            }
        }).disposed(by: globalDisposedBag)
    }
    
    /// 查询俱乐部信息
    /// - Parameters:
    ///   - requestParam: 请求参数
    ///   - callBack: 回调
    class func queryClubInfo(provider: MTProvider<MTClubApi>,
                          requestParam: [String : Any],
                          complete: @escaping (MTEditClubInfoModel?) -> Swift.Void,
                          failure: @escaping (AnyError) -> Swift.Void) {
        provider.rx.requestResult(.getClubInfo(requestParam)).subscribe(onNext: { (result:Result<MTEditClubInfoModel, AnyError> , isCache:Bool) in
            switch result {
            case .success(let m):
                complete(m)
            case .failure(let error):
                failure(error)
                break
            }
        }).disposed(by: globalDisposedBag)
    }
    
    /// - 编辑俱乐部
    /// - Parameters:
    ///   - requestParam: 请求参数
    ///   - callBack: 回调
    class func editClub(provider: MTProvider<MTClubApi>,
                          requestParam: [String : Any],
                          complete: @escaping (BaseComponentModel?) -> Swift.Void,
                          failure: @escaping (AnyError) -> Swift.Void) {
        provider.rx.requestResult(.editClub(requestParam)).subscribe(onNext: { (result:Result<BaseComponentModel, AnyError> , isCache:Bool) in
            switch result {
            case .success(let m):
                complete(m)
            case .failure(let error):
                failure(error)
                break
            }
        }).disposed(by: globalDisposedBag)
    }
    
}

extension MTClubTool {
    
    /// 申请接入俱乐部活动
    class func enroll(provider: MTProvider<MTClubApi>,
                      requestParam: [String : Any],
                      complete: @escaping (BaseComponentModel?) -> Swift.Void,
                      failure: @escaping (AnyError) -> Swift.Void) {
        
        provider.rx.requestResult(.enroll(requestParam)).subscribe(onNext: { (result:Result<BaseComponentModel, AnyError> , isCache:Bool) in
            switch result {
            case .success(let m):
                complete(m)
            case .failure(let error):
                failure(error)
                break
            }
        }).disposed(by: globalDisposedBag)
        
    }
    
    /// 查询创建俱乐部 / 俱乐部活动 / 用户余额
    class func config(provider: MTProvider<MTClubApi>,
                               complete: @escaping (MTClubConfigModel?) -> Swift.Void,
                               failure: @escaping (AnyError) -> Swift.Void) {
        
        provider.rx.requestResult(.config).subscribe(onNext: { (result:Result<MTClubConfigModel, AnyError> , isCache:Bool) in
            switch result {
            case .success(let m):
                complete(m)
            case .failure(let error):
                failure(error)
                break
            }
        }).disposed(by: globalDisposedBag)
        
    }
    
    
    /// 操作俱乐部成员 - 后台回传接口
    private class func operateClubUser(provider: MTProvider<MTClubApi>,
                                       requestParam: [String : Any],
                                       complete: @escaping (BaseComponentModel?) -> Swift.Void,
                                       failure: @escaping (AnyError) -> Swift.Void) {
        
        provider.rx.requestResult(.operateClubUser(requestParam)).subscribe(onNext: { (result:Result<BaseComponentModel, AnyError> , isCache:Bool) in
            switch result {
            case .success(let m):
                complete(m)
            case .failure(let error):
                failure(error)
                break
            }
        }).disposed(by: globalDisposedBag)
        
    }
    
    /// 操作俱乐部活动成员 - 后台回传接口
    private class func operateClubActivityUser(provider: MTProvider<MTClubApi>,
                                               requestParam: [String : Any],
                                               complete: @escaping (BaseComponentModel?) -> Swift.Void,
                                               failure: @escaping (AnyError) -> Swift.Void) {
        
        provider.rx.requestResult(.operateClubActivityUser(requestParam)).subscribe(onNext: { (result:Result<BaseComponentModel, AnyError> , isCache:Bool) in
            switch result {
            case .success(let m):
                complete(m)
            case .failure(let error):
                failure(error)
                break
            }
        }).disposed(by: globalDisposedBag)
        
    }
    
    /// 查询俱乐部活动详情
    class func queryActivityInfo(provider: MTProvider<MTClubApi>,
                                 requestParam: [String : Any],
                                 complete: @escaping (MTClubActivitiesDetailModel?) -> Swift.Void,
                                 failure: @escaping (AnyError) -> Swift.Void) {
        
        provider.rx.requestResult(.queryActivityInfo(requestParam)).subscribe(onNext: { (result:Result<MTClubActivitiesDetailModel, AnyError> , isCache:Bool) in
            switch result {
            case .success(let m):
                complete(m)
            case .failure(let error):
                failure(error)
                break
            }
        }).disposed(by: globalDisposedBag)
        
    }
    
    /// 创建俱乐部所需钻石
    class func createClubConsumeMoney(provider: MTProvider<MTClubApi>,
                         complete: @escaping (MTQueryClubMoneyModel?) -> Swift.Void,
                         failure: @escaping (AnyError) -> Swift.Void) {
        
        provider.rx.requestResult(.createClubConsumeMoney).subscribe(onNext: { (result:Result<MTQueryClubMoneyModel, AnyError> , isCache:Bool) in
            switch result {
            case .success(let m):
                complete(m)
            case .failure(let error):
                failure(error)
                break
            }
        }).disposed(by: globalDisposedBag)
        
    }
    
    /// 创建俱乐部活动所需钻石
    class func createClubActivityConsumeMoney(provider: MTProvider<MTClubApi>,
                         complete: @escaping (MTQueryClubMoneyModel?) -> Swift.Void,
                         failure: @escaping (AnyError) -> Swift.Void) {
        
        provider.rx.requestResult(.createActivityConsumeMoney).subscribe(onNext: { (result:Result<MTQueryClubMoneyModel, AnyError> , isCache:Bool) in
            switch result {
            case .success(let m):
                complete(m)
            case .failure(let error):
                failure(error)
                break
            }
        }).disposed(by: globalDisposedBag)
        
    }
    
    
    /// 查询俱乐部所有活动
    /// - Parameters:
    ///   - provider: provider description
    ///   - requestParam: requestParam description
    ///   - complete: complete description
    ///   - failure: failure description
    class func queryClubActivities(provider: MTProvider<MTClubApi>,
                                   requestParam: [String : Any],
                                   complete: @escaping (MTClubActivitiesModel?) -> Swift.Void,
                                   failure: @escaping (AnyError) -> Swift.Void) {
        
        provider.rx.requestResult(.queryClubActivities(requestParam)).subscribe(onNext: { (result:Result<MTClubActivitiesModel, AnyError> , isCache:Bool) in
            switch result {
            case .success(let m):
                complete(m)
            case .failure(let error):
                failure(error)
                break
            }
        }).disposed(by: globalDisposedBag)
        
    }
    
    /// 查询俱乐部进行中的活动
    /// - Parameters:
    ///   - provider: provider description
    ///   - requestParam: requestParam description
    ///   - complete: complete description
    ///   - failure: failure description
    class func queryEnrollRun(provider: MTProvider<MTClubApi>,
                         requestParam: [String : Any],
                         complete: @escaping (MTClubActivitiesModel?) -> Swift.Void,
                         failure: @escaping (AnyError) -> Swift.Void) {
        
        provider.rx.requestResult(.queryEnrollRun(requestParam)).subscribe(onNext: { (result:Result<MTClubActivitiesModel, AnyError> , isCache:Bool) in
            switch result {
            case .success(let m):
                complete(m)
            case .failure(let error):
                failure(error)
                break
            }
        }).disposed(by: globalDisposedBag)
        
    }
    
    /// 查询俱乐部结束的活动
    /// - Parameters:
    ///   - provider: provider description
    ///   - requestParam: requestParam description
    ///   - complete: complete description
    ///   - failure: failure description
    class func queryEnrollEnd(provider: MTProvider<MTClubApi>,
                         requestParam: [String : Any],
                         complete: @escaping (MTClubActivitiesModel?) -> Swift.Void,
                         failure: @escaping (AnyError) -> Swift.Void) {
        
        provider.rx.requestResult(.queryEnrollEnd(requestParam)).subscribe(onNext: { (result:Result<MTClubActivitiesModel, AnyError> , isCache:Bool) in
            switch result {
            case .success(let m):
                complete(m)
            case .failure(let error):
                failure(error)
                break
            }
        }).disposed(by: globalDisposedBag)
        
    }
    
    /// 查询土豪榜
    /// - Parameters:
    ///   - provider: provider description
    ///   - requestParam: requestParam description
    ///   - complete: complete description
    ///   - failure: failure description
    class func queryRich(provider: MTProvider<MTClubApi>,
                         requestParam: [String : Any],
                         complete: @escaping (MTClubRichListModel?) -> Swift.Void,
                         failure: @escaping (AnyError) -> Swift.Void) {
        
        provider.rx.requestResult(.queryRich(requestParam)).subscribe(onNext: { (result:Result<MTClubRichListModel, AnyError> , isCache:Bool) in
            switch result {
            case .success(let m):
                complete(m)
            case .failure(let error):
                failure(error)
                break
            }
        }).disposed(by: globalDisposedBag)
        
    }
    
    /// 查询最新俱乐部
    /// - Parameters:
    ///   - provider: provider description
    ///   - requestParam: requestParam description
    ///   - complete: complete description
    ///   - failure: failure description
    class func queryLester(provider: MTProvider<MTClubApi>,
                         requestParam: [String : Any],
                         complete: @escaping (MTClubRichListModel?) -> Swift.Void,
                         failure: @escaping (AnyError) -> Swift.Void) {
        
        provider.rx.requestResult(.queryLester(requestParam)).subscribe(onNext: { (result:Result<MTClubRichListModel, AnyError> , isCache:Bool) in
            switch result {
            case .success(let m):
                complete(m)
            case .failure(let error):
                failure(error)
                break
            }
        }).disposed(by: globalDisposedBag)
        
    }
    
    
    /// 查询用户余额
    /// - Parameters:
    ///   - provider: provider description
    ///   - requestParam: requestParam description
    ///   - complete: complete description
    ///   - failure: failure description
    class func queryUserMoney(provider: MTProvider<UserCenterApi>,
                         complete: @escaping (MTUserMoneyModel?) -> Swift.Void,
                         failure: @escaping (AnyError) -> Swift.Void) {
        
        provider.rx.requestResult(.queryMoney).subscribe(onNext: { (result:Result<MTUserMoneyModel, AnyError> , isCache:Bool) in
            switch result {
            case .success(let m):
                complete(m)
            case .failure(let error):
                failure(error)
                break
            }
        }).disposed(by: globalDisposedBag)
        
    }
    
}
